package com.pjchat.pjchatclient.handlerImpl;

import java.io.BufferedReader;
import java.io.BufferedWriter;

import javax.swing.JFrame;

import com.ita.im.model.Message;
import com.ita.im.model.Unit;
import com.pjchat.pjchatclient.frame.MyClientFrame;
import com.pjchat.pjchatclient.handler.BasicHandler;
import com.pjchat.pjchatclient.printer.ClientFramePrinter;
import com.pjchat.pjchatclient.sender.ForwardSender;

public class HandlerForMessage extends BasicHandler {
	private static HandlerForMessage instance;

	private HandlerForMessage() {

	}

	public static HandlerForMessage getInstance() {
		if (instance == null) {
			instance = new HandlerForMessage();
		}
		return instance;
	}

	@Override
	public Unit doRequest(JFrame frame, Unit unit, BufferedReader br,
			BufferedWriter bw) {
		ForwardSender.send(bw, unit);
		return null;
	}

	@Override
	public Unit doResponse(JFrame frame, Unit unit, BufferedReader br,
			BufferedWriter bw) {
		MyClientFrame myFrame = (MyClientFrame) frame;
		Message message = (Message) unit.getValue().get("message");
		ClientFramePrinter.getinstance().printMessage(myFrame.getMyStage(),
				message);
		if (message.isShake()) {
			myFrame.shake();
		}
		return null;
	}
}
